package com.gupao.jly521.core;

import com.gupao.jly521.observer.SubjectType;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jly
 * @date 2018/9/4  14:50
 */
public class EventListener {

    Map<Enum ,Event> eventMap = new HashMap();

    public void addListener(Object target, Method callBackMethod,SubjectType type){
        eventMap.put(type,new Event(this, target, callBackMethod));
    }

    public void trigger(Event e){
        try {
            e.getCallBack().invoke(e.getTarget(),e);
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        }
    }

    public void trigger(Enum e){
        if(e == null || eventMap.get(e) ==null){
            return;
        }
        trigger(eventMap.get(e));
    }

}
